//
//  FamilyManageVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/28.
//  Copyright © 2020 tym. All rights reserved.
//

#import "BlacklistVC.h"
#import "BlacklistCell.h"
#import "Sizes.h"
#import "ShowAlertView.h"
#import "FamilyOptionView.h"
#import "FamilyDescVC.h"
#import "LYEmptyViewHeader.h"
#import "ChangeFamilyVC.h"
#import "UserTool.h"
#import "UserModel.h"
#import "FamilyService.h"
#import "FamilyNameVC.h"
#import "FamilyDetailVC.h"
#import "BlacklistVC.h"
#import "FamilymemberVC.h"
#import "Colors.h"
#import "WHToast.h"
#import "Fonts.h"
#import "TYMEmptyView.h"
#import "TYMButton.h"
#import "TYMBaseTableViewCell.h"
#import "FamilyManageTwoCell.h"
#import "FamilyManageOneCell.h"
#import "FamilyService.h"
#import "GroupShadowTableView.h"
#import "FamilyManageThreeCell.h"
@interface BlacklistVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;

@property(nonatomic,strong) NSMutableArray *dataArray;


@property(nonatomic,strong) FamilyService *familyService;

@end

@implementation BlacklistVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"黑名单成员"];
    [self setTable];
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img2"
     titleStr:@"您还没有添加黑名单用户"
    detailStr:@""];
    [self getdataArrayWithWeb];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    
    _tableView.groupShadowDataSource = self;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = viewBGColor;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BlacklistCell  class]) bundle:nil] forCellReuseIdentifier:[BlacklistCell identifier]];
    
    [self.view addSubview:_tableView];
    
}

-(FamilyService*)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

-(void)rightFunc:(id)sender{
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)getdataArrayWithWeb{
    //家长
    TYMWeak(self);
    [self.familyService getFamilyuserfindfamilyusers:[UserTool getCurrentFamily].familyId status:3 type:2 completion:^(NSError * _Nonnull error, NSArray<UserModel *> * _Nonnull array) {
        if (weakself.dataArray.count ) {
            [weakself.dataArray removeAllObjects];
        }
        [weakself.dataArray addObjectsFromArray:array];
        [weakself.tableView reloadData];
        if (weakself.dataArray.count == 0) {
            [weakself.tableView ly_showEmptyView];
            weakself.tableView.isShowArray = @[@(0)];
        }else{
            weakself.tableView.isShowArray = @[@(1)];
            weakself.tableView.headTitles = @[@"黑名单成员"];
            weakself.tableView.headTitles = @[@"黑名单成员"];
            [weakself.tableView reloadData];
        }
    }];
    
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BlacklistCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BlacklistCell *cell = [tableView dequeueReusableCellWithIdentifier:[BlacklistCell identifier]];
    if (self.dataArray.count) {
        cell.userModel = self.dataArray[indexPath.row];
    }
    cell.deleteUserBlock = ^{
        [self deleteUserWithIndex:indexPath.row];
    };
    return cell;
}

-(void)deleteUserWithIndex:(NSInteger)index{
    UserModel *model = self.dataArray[index];
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定将该用户移除黑名单吗?" actionBtnTitle:@"确定"
                           hiddenCancel:NO
                           completion:^(NSInteger actionType) {
        if (actionType) {
            [weakself.familyService getFamilyuserdeletefamilyusers:model.familyUserId completion:^(LMJBaseResponse * _Nonnull respons) {
                if(!respons.message){
                     [self.dataArray removeObjectAtIndex:index];
                     [self.tableView reloadData];
                }
            }];
        }
    }];
}

@end
